/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.app.dao.impl;

import javax.persistence.NoResultException;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.apache.log4j.Logger;
import org.app.dao.AbstractBaseDAO;
import static org.app.dao.AbstractBaseDAO.em;
import org.app.dao.M_WebsiteAccountDAO;
import org.app.model.M_WebsiteAccount;
import org.app.model.M_WebsiteAccount_;

/**
 *
 * @author mrapple
 */
public class M_WebsiteAccountDAOImpl extends AbstractBaseDAO<M_WebsiteAccount> implements M_WebsiteAccountDAO {

    private static Logger logger = Logger.getLogger(M_WebsiteAccountDAOImpl.class);

    public M_WebsiteAccountDAOImpl() {
        super();
    }

    @Override
    public M_WebsiteAccount getWebsiteByDomain(String domain) throws Exception {
        M_WebsiteAccount website = new M_WebsiteAccount();
        try {
            CriteriaQuery<M_WebsiteAccount> criteria = builder.createQuery(M_WebsiteAccount.class);
            Root<M_WebsiteAccount> m_WebsiteRoot = criteria.from(M_WebsiteAccount.class);
            criteria.select(m_WebsiteRoot);
            criteria.where(builder.equal(m_WebsiteRoot.get(M_WebsiteAccount_.domainName), domain));
            website = em.createQuery(criteria).getSingleResult();
        } catch (NoResultException noResult) {
            logger.error("Domain : " + domain + " has no result . Ex : " + noResult.getMessage());
            website = null;
        } catch (Exception e) {
            throw e;
        }
        return website;
    }
}
